package com.xzy.sms.web.system.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xzy.sms.core.adapter.SendSmsAdapter;
import com.xzy.sms.web.domain.entity.ChannelConfig;
import com.xzy.sms.web.domain.service.ChannelConfigService;
import com.xzy.sms.web.rest.RestObject;

/**
 * @desc 
 * @author WuJiangWu
 * @date 2018年7月15日
 */
@Controller
@RequestMapping(value = "/admin/channelconfig")
public class ChannelConfigController{
    
    /** 日志打印器 */
    protected static Logger logger = LoggerFactory.getLogger(ChannelConfigController.class);
    
    /** channelconfig服务 */
    @Autowired
    private ChannelConfigService channelConfigService;
    
    /**
     * 配置信息列表
     * 
     * @param modelMap 模型
     * @return 视图
     */
    @RequestMapping(value = "")
    public String configList(ModelMap modelMap) {
        return "channelconfig/list";
    }
    
    /**
     * 配置信息列表分页
     * 
     * @param queryLike 查询条件
     * @param pageNumber 页数
     * @param pageSize 条数
     * @param model model
     * @return String
     */
    @RequestMapping(value = "/page")
    public String pageList(String queryLike, @RequestParam(defaultValue = "1") int pageNumber, 
    		@RequestParam(defaultValue = "20") int pageSize,
        ModelMap model) {
        Map<String, Object> filter = new HashMap<>();
        int page = pageNumber < 0 ? 1 : pageNumber;
        Page<ChannelConfig> list = null;
        
        if (StringUtils.isNotBlank(queryLike)) {
            filter.put("LIKE_title", queryLike);
        }
        list = channelConfigService.findAll(filter, page, pageSize);
        
        model.put("data", list);
        return "channelconfig/_page";
    }
    
    /**
     * 详细信息
     * 
     * @param channelconfigId 通道id
     * @param model model
     * @return 视图
     */
    @RequestMapping(value = "/detail")
    public String userDetail(String channelconfigId, ModelMap model) {
        if (StringUtils.isNotBlank(channelconfigId)) {
            model.put("channelConfig", channelConfigService.getOne(channelconfigId));
        }
        return "channelconfig/form";
    }
    
    /**
     * 保存通道
     * 
     * @param channelconfig xx
     * @return 视图
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public RestObject save(ChannelConfig channelconfig) {
        ChannelConfig newChannelConfig = null;
        String adapterClass = channelconfig.getSendAdapterClass();
        if (StringUtils.isBlank(adapterClass)) {
            return RestObject.newError("code不能为空");
        }
        
        try {
            Class<?> clazz = Class.forName(adapterClass);
            Object obj = clazz.newInstance();
            if (obj == null || !(obj instanceof SendSmsAdapter)) {
                return RestObject.newError("该类不是有效的适配器。");
            }
        } catch (ClassNotFoundException e) {
            return RestObject.newError("找不到相应的适配器。");
        } catch (InstantiationException | IllegalAccessException e) {
            return RestObject.newError("该适配器类不能够实例化。");
        }
        
        try {
            newChannelConfig = channelConfigService.save(channelconfig);
        } catch (Exception ex) {
            logger.error("在保存通道信息时异常", ex);
            return RestObject.newError(ex.getMessage());
        }
        return RestObject.newOk(newChannelConfig);
    }
    
    /**
     * 删除通道
     * @param channelconfigId xx
     * @return 视图
     */
    @RequestMapping(value = "/del")
    @ResponseBody
    public RestObject del(String channelconfigId) {
        try {
            if (StringUtils.isNotEmpty(channelconfigId)) {
                int count = channelConfigService.queryApplicationCount(channelconfigId);
                if (count > 0) {
                    return RestObject.newError("通道正被" + count + "个应用使用，不能被删除");
                }
                channelConfigService.delete(channelconfigId);
            }
        } catch (Exception ex) {
            logger.error("在删除通道信息时异常", ex);
            return RestObject.newError(ex.getMessage());
        }
        return RestObject.newOk("success");
    }
    
}
